import request from "../request";

// 医生预约时间段接口类型定义
interface ClinicTimeSlot {
  id: string;
  clnicTime: string; // 诊所时间
  staffId: string;   // 医生ID
  period: string;    // 时段（上午/下午）
  typeId: number;    // 类型ID
  slotTotal: number; // 总号源
  slotUse: number;   // 已使用号源
  status: number;    // 状态
  staffDetailVo: any | null;
}

interface ClinicTimeSlotsResponse {
  code: number;
  msg?: string;
  data: ClinicTimeSlot[];
}

// 获取医生预约时间段
const getClinicTimeSlots = async (staffId: string): Promise<ClinicTimeSlotsResponse> => {
  const data = await request({
    url: `/appointment/clinicStaffStaffId`,
    method: "GET",
    data: {
      staffId: staffId,
      typeId: 1  
    }
  });
  return data;
};

// 创建预约请求参数类型
interface CreateAppointmentRequest {
  staffId: number;      // 医生id
  patientId: number;    // 患者id
  typeId: number;       // 预约挂号是1
  userId: number;       // 用户id
  clnicId: number;      // 坐诊id
  bookedAt: string;     // 预约日期
}

// 创建预约返回数据类型
interface AppointmentData {
  id: string;
  channel: number;
  clnicId: string;
  patientId: string;
  staffId: string;
  slotNo: number;
  status: number;
  orderId: string;
  bookedAt: string;
  cancelReason: string | null;
  createdAt: string | null;
  updatedAt: string | null;
  userId: string;
}

interface CreateAppointmentResponse {
  code: number;
  msg?: string;
  data: AppointmentData;
}

// 创建预约
const createAppointment = async (params: CreateAppointmentRequest): Promise<CreateAppointmentResponse> => {
  const data = await request({
    url: "/appointment/appointment",
    method: "POST",
    data: params
  });
  return data;
};

// 取消预约请求参数类型
interface CancelAppointmentRequest {
  id: string;           // 预约单id
  staffId: number;      // 医生id
  patientId: number;    // 患者id
  userId: number;       // 用户id
  clnicId: number;      // 医生坐诊id
  slotNo: number;       // 号
}

// 取消预约返回数据类型
interface CancelAppointmentResponse {
  code: number;
  msg?: string;
  data: boolean;
}

// 取消预约
const cancelAppointment = async (params: CancelAppointmentRequest): Promise<CancelAppointmentResponse> => {
  const data = await request({
    url: "/appointment/cancel",
    method: "POST",
    data: params
  });
  return data;
};

// 挂号历史请求参数类型
interface GetMyAppointmentsRequest {
  userId: string;
}

// 挂号历史返回数据类型
interface MyAppointmentData {
  id: string;
  staffName: string;
  deptName: string;
  orderId: string;
  bookedAt: string;
  slotNo: number;
  status: number;
  userId: string;
}

interface GetMyAppointmentsResponse {
  code: number;
  msg?: string;
  data: MyAppointmentData[];
}

// 获取我的挂号历史
const getMyAppointments = async (userId: string): Promise<GetMyAppointmentsResponse> => {
  const data = await request({
    url: "/appointment/getMyAppointments",
    method: "GET",
    data: { userId }
  })
  return data
}

// 获取医生排班 - 请求与响应类型
interface GetClinicStaffRequest {
  typeId: number;
  deptId: number;
  clnicTime: string;
}

interface GetClinicStaffStaffDetailVo {
  staffId: string;
  staffName: string;
  positionName: string;
  titleName: string;
  titleLevel: string;
  avatar: string | null;
}

interface GetClinicStaffItem {
  id: string;
  clnicTime: string;
  staffId: string;
  period: string;
  typeId: number;
  slotTotal: number;
  slotUse: number;
  status: number;
  staffDetailVo: GetClinicStaffStaffDetailVo | null;
}

interface GetClinicStaffResponse {
  code: number;
  msg?: string;
  data: GetClinicStaffItem[];
}

// 统一走 BASE_API_URL
const getClinicStaff = async (params: GetClinicStaffRequest): Promise<GetClinicStaffResponse> => {
  const data = await request({
    url: "/appointment/getClinicStaff",
    method: "POST",
    data: params
  });
  return data;
};

// api/order.ts
import request from '@/request'

export interface OrderDetail {
  id: string
  orderNo: string
  userId: string
  orderType: number
  amount: number
  status: number
  payDeadline: string
  createdAt: string
}

// 预约挂号 typeId 固定为 0
export const getOrder = async (orderId: string, typeId = 0) => {
  return await request<{ code: number; msg?: string; data: OrderDetail }>({
    url: `/order/getOrder/${orderId}/${typeId}`,
    method: 'GET'
  })
}

// 立即到诊请求参数类型
interface PutQueueRequest {
  id: string;        // 预约单号
  staffId:string,
  clnicId: number;   // 必填
  slotNo: number;    // 必填
}

// 立即到诊返回数据类型
interface PutQueueResponse {
  code: number;
  msg: string;
}

// 立即到诊接口
const putQueue = async (params: PutQueueRequest): Promise<PutQueueResponse> => {
  const data = await request({
    url: "/appointment/putQueue",
    method: "POST",
    data: params
  });
  return data;
};

export { getClinicTimeSlots, createAppointment, cancelAppointment, getMyAppointments, getClinicStaff, putQueue };
export type {
  ClinicTimeSlot, ClinicTimeSlotsResponse,
  CreateAppointmentRequest, AppointmentData, CreateAppointmentResponse,
  CancelAppointmentRequest, CancelAppointmentResponse,
  GetMyAppointmentsRequest, MyAppointmentData, GetMyAppointmentsResponse,
  GetClinicStaffRequest, GetClinicStaffItem, GetClinicStaffResponse,
  PutQueueRequest, PutQueueResponse
};
